package com.expoente.view;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.expoente.DroidBusConstants;
import com.expoente.model.LineInformation;
import com.expoente.model.dao.impl.DBAdapterImpl;
import com.expoente.utils.DroidBusUtils;

public class LineSearchActivity
		extends Activity {

	private Spinner lineSpinner;
	private ArrayList<LineInformation> lines;
	private Location lastLocation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.line_search_activity);

		Location l = DroidBusUtils.location;
		lastLocation = DroidBusUtils.getLocation(this);

		Button searchByLocationButton = (Button) findViewById(R.id.searchByLocationButton);

		if (null == lastLocation) {
			searchByLocationButton.setEnabled(false);
			TextView gpsDisabledTextView = (TextView) findViewById(R.id.gpsDisabled);
			gpsDisabledTextView.setVisibility(View.VISIBLE);
		}

		AssetManager assetManager = getAssets();
		DBAdapterImpl dbAdapter = new DBAdapterImpl(this);
		dbAdapter.open();
		try {
			dbAdapter.resetDatabase(DroidBusUtils
					.getLinesFromProperties(assetManager
							.open(DroidBusConstants.LINES_PROPERTIES)),
					DroidBusUtils.getPlacesFromProperties(assetManager
							.open(DroidBusConstants.PLACES_PROPERTIES)),
					DroidBusUtils.getRoutesFromProperties(assetManager
							.open(DroidBusConstants.ROUTES_PROPERTIES)));
		} catch (Exception e) {
			Log.e(DroidBusConstants.RESET_DATABASE, e.getMessage());
		}
		lines = dbAdapter.getAllLinesInformation();
		dbAdapter.close();

		searchByLocationButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				Intent intent = new Intent(view.getContext(),
						LocationLinesActivity.class);
				intent.putExtra("location", lastLocation);
				intent.putExtra("lines", lines);
				startActivity(intent);
			}
		});

		lineSpinner = (Spinner) findViewById(R.id.lineSpinner);
		ArrayAdapter<LineInformation> adapter = new ArrayAdapter<LineInformation>(
				this, android.R.layout.simple_spinner_dropdown_item, lines);
		adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		lineSpinner.setAdapter(adapter);

		Button viewDetailsButton = (Button) findViewById(R.id.viewDetailsButton);
		viewDetailsButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				Intent intent = new Intent(view.getContext(),
						LineReviewActivity.class);
				LineInformation lineInformation = (LineInformation) lineSpinner
						.getSelectedItem();
				intent.putExtra("item", lineInformation);
				startActivity(intent);
			}
		});

	}

	public Spinner getLineSpinner() {
		return lineSpinner;
	}

	public void setLineSpinner(Spinner lineSpinner) {
		this.lineSpinner = lineSpinner;
	}
}
